package games.checkers;

import games.checkers.chipPath.pathNode;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class boardView extends JFrame 
{
	public static class miniBoard extends JPanel
	{
		public class miniTile extends Rectangle
		{
			public boolean highlight = false;
			private Color color = new Color(0,130,0);
			private Color hcolor = new Color(0,255,0);
			public int chip = 0;
			private Color p1color = new Color(255,0,0);
			private Color p2color = new Color(255,255,255);
			private Color king = new Color(255,215,0);
			private int chipSize = 0;
			private int kingSize = 0;
			
			public miniTile(int x, int y, int width, int height, int c)
			{
				super(x,y,width,height);
				chip = c;
				chipSize = (int)(width/1.5);
				kingSize = chipSize/2;
			}
			
			public void paint(Graphics g)
			{
				Graphics2D g2d = (Graphics2D) g;
				
				if (highlight) g2d.setColor(hcolor); else g2d.setColor(color);
				g2d.fill(this);
				
				if (chip > 0)
				{
					g2d.setColor(p2color);
					g2d.fill(new Ellipse2D.Float(x+(width/2-chipSize/2),y+(height/2-chipSize/2), chipSize, chipSize));
					if (chip > 1)
					{
						g2d.setColor(king);
						g2d.fill(new Ellipse2D.Float(x+(width/2-kingSize/2),y+(height/2-kingSize/2), kingSize, kingSize));
					}
				}
				if (chip < 0)
				{
					g2d.setColor(p1color);
					g2d.fill(new Ellipse2D.Float(x+(width/2-chipSize/2),y+(height/2-chipSize/2), chipSize, chipSize));
					if (chip < -1)
					{
						g2d.setColor(king);
						g2d.fill(new Ellipse2D.Float(x+(width/2-kingSize/2),y+(height/2-kingSize/2), kingSize, kingSize));
					}
				}
				
			}	
			
		}
		private int [] boardLayout;
		private ArrayList<pathNode> path = new ArrayList<pathNode>(0);
		private ArrayList<miniTile> tiles = new ArrayList<miniTile>(32);
		
		public miniBoard()
		{ 
			tiles.clear(); 
			 int [] tileLocX = new int []
				              {1,3,5,7,
						       0,2,4,6,
						       1,3,5,7,
						       0,2,4,6,
						       1,3,5,7,
						       0,2,4,6,
						       1,3,5,7,
						       0,2,4,6};
			 int [] tileLocY = new int []
				              {0,0,0,0, 
						       1,1,1,1,
						       2,2,2,2,
						       3,3,3,3,
						       4,4,4,4,
						       5,5,5,5,
						       6,6,6,6,
						       7,7,7,7};

			boardLayout = new int []
			                   {1,1,1,1,
					            1,1,1,1,
					            1,1,1,1,
					            0,0,0,0,
					            0,0,0,0,
					            -1,-1,-1,-1,
					            -1,-1,-1,-1,
					            -1,-1,-1,-1};
			 
			 for (int i=0; i<32; i++) tiles.add(new miniTile(tileLocX[i]*31,tileLocY[i]*31,32,32,boardLayout[i]));
			 
			
		}
		
		public void updateBoard(int [] board, ArrayList<pathNode> p)
		{
			miniTile t;
			boardLayout = board;
			path = p;
			for (int i=0; i < 32; i++)
			{
				t = tiles.get(i);
				t.highlight = false;
				t.chip = board[i];
			}
			
			for (int j=0; j < path.size();j++)
			{
				tiles.get(path.get(j).tile).highlight = true;
			}
			
			repaint();
		}
		
		public int[] boardLayout()
		{
			return boardLayout;
		}
		
		public ArrayList<pathNode> path()
		{
			return path;
		}
		
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			
			for (int i=0; i<32; i++) tiles.get(i).paint(g);
			
		}
	}
	
	private miniBoard board1;
	private miniBoard board2;
	private miniBoard board3;

	public boardView(checkers game)
	{
		Dimension size = new Dimension(260,284);
		
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		board1 = new miniBoard();
		board1.setPreferredSize(size);
		board1.setBackground(new Color(240,220,130));
		add(board1);
		
		board2 = new miniBoard();
		board2.setPreferredSize(size);
		board2.setBackground(new Color(240,220,130));
		add(board2);
		
//		board3 = new miniBoard();
//		board3.setPreferredSize(size);
//		board3.setBackground(new Color(240,220,130));
//		add(board3);
		
		setSize(new Dimension(size.width,size.height*2));
		setTitle("Previous Move");
		
		//center on screen
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension program = this.getSize();
		Dimension gamedims = game.getSize();
		Point gameloc = game.getLocation();
		gameloc.x -= program.width+20;
		gameloc.y += (gamedims.height-program.height)/2;
		this.setLocation(gameloc);
		setVisible(true);
	}
	
	public void updateBoard(int [] b, ArrayList<pathNode> path)
	{
		//board3.updateBoard(board2.boardLayout(),board2.path());
		board2.updateBoard(board1.boardLayout(), board1.path());
		board1.updateBoard(b, path);
		
	}
	
}
